package com.jdz.controller.portal;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jdz.common.Const;
import com.jdz.common.ResponseCode;
import com.jdz.common.ServerResponse;
import com.jdz.pojo.ProjectDetails;
import com.jdz.service.IFileService;
import com.jdz.service.IProjectDetailsService;
import com.jdz.service.IUserService;
import com.jdz.vo.UserVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by sun
 **/
@Controller
@RequestMapping("/projectDetails")
public class ProjectDetailsController {

    @Autowired
    IFileService iFileService;
    @Autowired
    IUserService iUserService;
    @Autowired
    IProjectDetailsService iProjectDetailsService;

//
////    当他为false 时  使用这个注解可以不传这个参数  true时必须传
////    required默认值是true
//
//    /**
//     * 上传文件
//     * @param session
//     * @param fileList
//     * @param request
//     * @return
//     */
//    @RequestMapping("upload.do")
//    @ResponseBody
//    public ServerResponse upload(HttpSession session, @RequestParam(value = "upload_file",required = false) List<MultipartFile> fileList, HttpServletRequest request){
//        UserVo uservo = (UserVo)session.getAttribute(Const.CURRENT_USER);
//        if(uservo == null){
//            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
//        }
//        List<Map> list = Lists.newArrayList();
//        if(!fileList.isEmpty()&&fileList.size()>0){
//            for(MultipartFile file : fileList) {
//                String path = request.getSession().getServletContext().getRealPath("upload");
//                String targetFileName = iFileService.uploadName(uservo.getStuid(),file, path);
//                String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/upload/" + targetFileName;
//
//                Map fileMap = Maps.newHashMap();
//                fileMap.put("uri", targetFileName);
//                fileMap.put("url", url);
//
//                list.add(fileMap);
//            }
//            return ServerResponse.createBySuccess(list);
//        }else {
//            return ServerResponse.createByErrorMessage("没有接收到文件");
//        }
//    }
//
//    /**
//     * wangEdier富文本上传图片
//     * @param session
//     * @param fileList
//     * @param request
//     * @param response
//     * @return
//     */
//    @RequestMapping("richtext_img_upload.do")
//    @ResponseBody
//    public Map richtextImgUpload(HttpSession session, @RequestParam(value = "upload_file",required = false) List<MultipartFile> fileList, HttpServletRequest request, HttpServletResponse response){
//        Map resultMap = Maps.newHashMap();
//        UserVo userVo = (UserVo)session.getAttribute(Const.CURRENT_USER);
//        if(userVo == null){
//            resultMap.put("errno",10);
//            return resultMap;
//        }
//        //富文本中对于返回值有自己的要求,我们使用是simditor所以按照simditor的要求进行返回
////        {
////            "success": true/false,
////                "msg": "error message", # optional
////            "file_path": "[real file path]"
////        }
////wangEditor
////        {
////            errno:0,//正确是0，错误是不等于0其他数字，自定义处理
////            data:["url1","url2"]
////        }
//
//
//        String path = request.getSession().getServletContext().getRealPath("upload");
//        List<String> list = Lists.newArrayList();
//        for(MultipartFile file :fileList){
//            String targetFileName = iFileService.uploadName(userVo.getStuid(),file,path);
//            String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/upload/"+targetFileName;
//            list.add(url);
//        }
//
//        if(CollectionUtils.isEmpty(list)||list==null){
//            resultMap.put("errno",1);
//            return resultMap;
//        }
//
//
//        resultMap.put("errno",0);
//        resultMap.put("data",list);
//
//        return resultMap;
//
//    }

    /**
     * 新增和修改项目
     * @param session
     * @param projectDetails
     * @return
     */
    @RequestMapping(value = "save.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse productSave(HttpSession session, ProjectDetails projectDetails){
        UserVo userVo = (UserVo) session.getAttribute(Const.CURRENT_USER);

        return iProjectDetailsService.saveOrUpdateProduct(projectDetails,userVo.getStuno());
    }

    /**
     * 用户自己的项目列表
     * @param session
     * @return
     */
    @RequestMapping(value = "list_my_product.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse listMyProduct(HttpSession session){
        UserVo userVo = (UserVo) session.getAttribute(Const.CURRENT_USER);
        return iProjectDetailsService.listMyProduct(userVo.getStuno());
    }

    /**
     * 项目详情
     * @param session
     * @param projectno
     * @return
     */
    @RequestMapping(value = "details_productt.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse detailsProduct(HttpSession session,Integer projectno){
        UserVo userVo = (UserVo) session.getAttribute(Const.CURRENT_USER);
        return iProjectDetailsService.detailsProduct(userVo.getStuno(),projectno);
    }


    /**
     * 优秀项目详情
     * @param session
     * @param projectno
     * @return
     */
    @RequestMapping(value = "excellent_works_details_productt.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse excellentWorksDetailsProduct(HttpSession session,Integer projectno){
        return iProjectDetailsService.excellentWorksDetailsProduct(projectno);
    }


    /**
     * 获取所有优秀项目
     * @returnt
     */
    @RequestMapping(value = "get_excellent_works.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getExcellentWorks() {

        return iProjectDetailsService.getExcellentWorks();
    }


}
